<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>

<head>
  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  <title>添加删除记录练习</title>
  <link rel="stylesheet" type="text/css" href="css.css"/>
</head>
<body>

<table id="employeeTable">
  <tr>
    <th>Name</th>
    <th>Email</th>
    <th>Salary</th>
    <th>&nbsp;</th>
  </tr>
  <tr>
    <td>Tom</td>
    <td>tom@tom.com</td>
    <td>5000</td>
    <td><a href="deleteEmp?id=001">Delete</a></td>
  </tr>
  <tr>
    <td>Jerry</td>
    <td>jerry@sohu.com</td>
    <td>8000</td>
    <td><a href="deleteEmp?id=002">Delete</a></td>
  </tr>
  <tr>
    <td>Bob</td>
    <td>bob@tom.com</td>
    <td>10000</td>
    <td><a href="deleteEmp?id=003">Delete</a></td>
  </tr>

</table>

<div id="formDiv">

  <h4>添加新员工</h4>

  <table>
    <tr>
      <td class="word">name:</td>
      <td class="inp">
        <input type="text" name="empName" id="empName"/>
      </td>
    </tr>
    <tr>
      <td class="word">email:</td>
      <td class="inp">
        <input type="text" name="email" id="email"/>
      </td>
    </tr>
    <tr>
      <td class="word">salary:</td>
      <td class="inp">
        <input type="text" name="salary" id="salary"/>
      </td>
    </tr>
    <tr>
      <td colspan="2" align="center">
        <button id="addEmpButton" value="abc">
          Submit
        </button>
      </td>
    </tr>
  </table>
</div>

<script type="text/javascript" src="../常见效果/jquery-1.10.1.js"></script>
<script>
  $(function () {
     //找到提交按钮
     var $addEmpButton = $('#addEmpButton')
     //找到tbody
     var $tbody = $('#employeeTable tbody')
    $addEmpButton.click(function () {
       //获取员工的信息
       var $empName = $(':text').filter('[name=empName]')
       var $email = $(':text').filter('[name=email]')
       var $salary = $(':text').filter('[name=salary]')

       var empName = $empName.val()
       var email = $email.val()
       var salary = $salary.val()

       $empName.val('')
       $email.val('')
       $salary.val('')

      //新建一条员工记录的tr
       var $newEmp = $('<tr></tr>')
       //往tr中添加信息
       $newEmp
         .append('<td>'+empName+'</td>')
         .append('<td>'+email+'</td>')
         .append('<td>'+salary+'</td>')
         .append('<td><a href="deleteEmp?id=003">Delete</a></td>')
         .find('a').click(delEmp)

       //给a标签绑定单击事件
       // $newEmp.find('a').click(delEmp)

       //将新员工的信息添加到table中去
       $newEmp.appendTo($tbody)
     })

    //将之前的员工记录也绑定上删除方法
    $tbody.find('a').click(delEmp)
  })




  //定义一个删除员工的方法
  function delEmp() {
    //提示用户
    var $tr = $(this).parent().parent()
    if (confirm("确定要删除"+ $tr.find(':first-child').html() +"的信息吗？")){
      $tr.remove()
      //组织默认行为（跳转）
    }
    event.preventDefault()

  }
</script>
</body>
</html>